from django.views.generic.base import View
from django.shortcuts import render
from PIL import Image
from django.http import HttpResponseRedirect
from django.urls import reverse
import numpy as np
import tensorflow as tf
import cv2


#
class ImageView(View):
    def get(self, request):
        left_image = 'media/image/logo.jpg'  # 默认图片
        return render(request, 'index.html', {'left_image': left_image, 'right_image': "media/target.png", 'result': '待识别'})

    def post(self, request):
        image = request.FILES.get('image',)

        if image:
            img_file = Image.open(image)
            img_file.save('media/' + str(image))
            left_image = 'media/' + str(image)  # 提交图片
        else:
            left_image = 'media/image/logo.jpg'  # 默认图片

        # right_image = 'media/image/默认.jpg'  # 默认图片
        left_image = 'media/image/logo.jpg'  # 默认图片
        right_image = 'media/' + str(image)  # 默认图片
        # 把读取到的图片保存在media目录下
        src_img= cv2.imread(right_image)
        target_img = cv2.resize(src_img, (224, 224))
        cv2.imwrite("media/target.png", target_img)
        return render(request, 'index.html', {'left_image': left_image, 'right_image': right_image, 'result': '待识别'})


class IdentifyView(View):
    def post(self, request):
        # 通过post方法提交图片之后需要调用模型对图片进行识别
        class_names = ['Apple Braeburn', 'Apple Crimson Snow', 'Apple Golden 1', 'Apple Golden 2', 'Apple Golden 3', 'Apple Granny Smith', 'Apple Pink Lady', 'Apple Red 1', 'Apple Red 2', 'Apple Red 3', 'Apple Red Delicious', 'Apple Red Yellow 1', 'Apple Red Yellow 2', 'Apricot', 'Avocado', 'Avocado ripe', 'Banana', 'Banana Lady Finger', 'Banana Red', 'Beetroot', 'Blueberry', 'Cactus fruit', 'Cantaloupe 1', 'Cantaloupe 2', 'Carambula', 'Cauliflower', 'Cherry 1', 'Cherry 2', 'Cherry Rainier', 'Cherry Wax Black', 'Cherry Wax Red', 'Cherry Wax Yellow', 'Chestnut', 'Clementine', 'Cocos', 'Corn', 'Corn Husk', 'Cucumber Ripe', 'Cucumber Ripe 2', 'Dates', 'Eggplant', 'Fig', 'Ginger Root', 'Granadilla', 'Grape Blue', 'Grape Pink', 'Grape White', 'Grape White 2', 'Grape White 3', 'Grape White 4', 'Grapefruit Pink', 'Grapefruit White', 'Guava', 'Hazelnut', 'Huckleberry', 'Kaki', 'Kiwi', 'Kohlrabi', 'Kumquats', 'Lemon', 'Lemon Meyer', 'Limes', 'Lychee', 'Mandarine', 'Mango', 'Mango Red', 'Mangostan', 'Maracuja', 'Melon Piel de Sapo', 'Mulberry', 'Nectarine', 'Nectarine Flat', 'Nut Forest', 'Nut Pecan', 'Onion Red', 'Onion Red Peeled', 'Onion White', 'Orange', 'Papaya', 'Passion Fruit', 'Peach', 'Peach 2', 'Peach Flat', 'Pear', 'Pear 2', 'Pear Abate', 'Pear Forelle', 'Pear Kaiser', 'Pear Monster', 'Pear Red', 'Pear Stone', 'Pear Williams', 'Pepino', 'Pepper Green', 'Pepper Orange', 'Pepper Red', 'Pepper Yellow', 'Physalis', 'Physalis with Husk', 'Pineapple', 'Pineapple Mini', 'Pitahaya Red', 'Plum', 'Plum 2', 'Plum 3', 'Pomegranate', 'Pomelo Sweetie', 'Potato Red', 'Potato Red Washed', 'Potato Sweet', 'Potato White', 'Quince', 'Rambutan', 'Raspberry', 'Redcurrant', 'Salak', 'Strawberry', 'Strawberry Wedge', 'Tamarillo', 'Tangelo', 'Tomato 1', 'Tomato 2', 'Tomato 3', 'Tomato 4', 'Tomato Cherry Red', 'Tomato Heart', 'Tomato Maroon', 'Tomato Yellow', 'Tomato not Ripened', 'Walnut', 'Watermelon']
        model = tf.keras.models.load_model("media/models/mobilenet_fruits.h5")
        img = Image.open('media/target.png')
        img = np.asarray(img)
        # gray_img = img.convert('L')
        # img_torch = self.transform(gray_img)
        outputs = model.predict(img.reshape(1, 224, 224, 3))
        # print(outputs)
        result_index = int(np.argmax(outputs))
        # print(result_index)
        result = class_names[result_index]
        print(result)
        result_str = "识别结果为：{}".format(result)
        # image = request.POST.get('image', )  # 识别图片

        return render(request, 'index.html', {'left_image': 'media/image/logo.jpg', 'right_image': "media/target.png", "result": result_str})